package cn.tedu.controller;

import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.Arrays;

@RestController
@RequestMapping("stu")
public class StudentController {
    @RequestMapping("add")
    public Object add(Student s) throws Exception {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取链接
        String url="jdbc:mysql://localhost:3306/cgb2106";
        Connection c = DriverManager.getConnection(url, "root", "root");
        //获取传输器
        String sql="insert into Student values (?,?,?,?,?,?,?)";
        PreparedStatement ps = c.prepareStatement(sql);
        //设置参数
        ps.setObject(1,null);
        ps.setObject(2,s.getName());
        ps.setObject(3,s.getAge());
        ps.setObject(4,s.getSex());
        ps.setObject(5, Arrays.toString(s.getHobby()).substring(1,Arrays.toString(s.getHobby()).length()-1));
        ps.setObject(6,s.getEdu());
        ps.setObject(7,s.getIntime());

        ps.executeUpdate();
        c.close();
        ps.close();

        return s;
    }
}
